package com.example.lotterysystem.controller;


import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtils;
import com.example.lotterysystem.controller.param.ShortMessageLoginParam;
import com.example.lotterysystem.controller.param.UserPasswordLoginParam;
import com.example.lotterysystem.controller.param.UserRegisterParam;
import com.example.lotterysystem.controller.result.UserBaseInfoResult;
import com.example.lotterysystem.controller.result.UserLoginResult;
import com.example.lotterysystem.controller.result.UserRegisterResult;
import com.example.lotterysystem.service.UserService;
import com.example.lotterysystem.service.VerificationCodeService;
import com.example.lotterysystem.service.dto.UserDTO;
import com.example.lotterysystem.service.dto.UserLoginDTO;
import com.example.lotterysystem.service.dto.UserRegisterDTO;
import com.example.lotterysystem.service.enums.UserIdentityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private VerificationCodeService verificationCodeService;
    @Autowired
    private UserService userService;
    /**
     * 用户注册
     * @param param
     * @return
     */
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> register(@Validated @RequestBody UserRegisterParam param) {
        //日志打印
        logger.info("userRegister UserRegisterParam{}", JacksonUtils.writeValueString(param));
        //调用service
        UserRegisterDTO userRegisterDTO = userService.register(param);
        return  CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }



    @RequestMapping("/base-user/find-list")
    public CommonResult<List<UserBaseInfoResult>> findUserBaseInfoList(String identity) {
        logger.info("findUserBaseInfoList identity{}",identity);
        List<UserDTO> userDTOList = userService.findUserBaseInfoList(UserIdentityEnum.forName(identity));
        return CommonResult.success(convertToList(userDTOList));
    }

    private List<UserBaseInfoResult> convertToList(List<UserDTO> userDTOList) {
        if(CollectionUtils.isEmpty(userDTOList)) {
            return Arrays.asList();
        }
        return userDTOList.stream()
                .map(UserDTO -> {
                    UserBaseInfoResult result = new UserBaseInfoResult();
                    result.setUserId(UserDTO.getUserId());
                    result.setUserName(UserDTO.getUserName());
                    result.setIdentity(UserDTO.getIdentity().name());
                    return result;
                }).collect(Collectors.toList());

    }
    


    /**
     * 短信登录
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMessageLogin(@Validated @RequestBody ShortMessageLoginParam param) {
        logger.info("shortMessageLogin ShortMessageLoginParam:{}",
                JacksonUtils.writeValueString(param));

        UserLoginDTO userLoginDto = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDto));
    }

    /**
     * 密码登录
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(@Validated @RequestBody UserPasswordLoginParam param) {
        logger.info("userPasswordLogin UserPasswordLoginParam:{}",
                JacksonUtils.writeValueString(param));

        UserLoginDTO userLoginDto = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDto));
    }

    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDto) {
        if(null == userLoginDto ) {
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        userLoginResult.setToken(userLoginDto.getToken());
        userLoginResult.setIdentity(userLoginDto.getIdentity().name());
        return userLoginResult;

    }



    /**
     * 发送验证码服务
     * @param phoneNumber
     * @return
     */
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber) {
        logger.info("sendVerificationCode phoneNumber{}",phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }

    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        UserRegisterResult result = new UserRegisterResult();
        if(null == userRegisterDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        result.setUserId(userRegisterDTO.getUserId());
        return result;
    }

}
